package com.sixth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.LinkDao;
import com.sixth.entity.Banner;
import com.sixth.entity.Link;
import com.sixth.entity.TableResult;
import com.sixth.service.LinkService;
import com.sixth.until.CommonResult;
import com.sixth.until.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: C
 * @create: 2022-04-21 17:23
 **/
@Service
public class LinkServicelmpl implements LinkService {
@Autowired
private LinkDao linkDao;
    @Override
    public TableResult selectAlllink(Integer pageNum, Integer pageSize, Link link) {
            PageHelper.startPage(pageNum,pageSize);
            String linkName = link.getLinkName();
            Integer isDisable= link.getIsDisable();
            System.out.println(linkName+isDisable);
            List<Link> links= linkDao.selectlinByCondition(linkName,isDisable);
            PageInfo<Link> pageInfo = new PageInfo<Link>(links);

            return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
        }

    @Override
    public CommonResult deleteFindById(Link link) {
          String linkid = link.getLinkId();
            int row =linkDao.deleteByPrimaryKey(linkid);
            if (row>0){
                return new CommonResult(2000,"删除成功","null");
            }else {
                return new CommonResult(5000,"删除失败","null");
            }
    }


    @Override
    public CommonResult addZone(Link link) {
        String id= OrderNoUtil.getOrderNo();
        int i = linkDao.addlink(id,link);
        if (i !=0){
            return new CommonResult(2000,"添加成功",null);
        }
        return new CommonResult(2000,"添加失败",null);

    }

    @Override
    public CommonResult UpdateZone(Link link) {
        int i =linkDao.updateByPrimaryKeySelective(link);
        if (i != 0) {
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
}

    @Override
    public CommonResult changeStatus(String linkId, Integer isDisable) {
        int i = linkDao.changeStatusByid(linkId, isDisable);
        if (i != 0) {
            return new CommonResult(2000,"修改状态成功",null);
        }
        return new CommonResult(2000,"修改失败",null);
    }

    @Override
    public CommonResult getQianLink() {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted","0");
        wrapper.eq("is_disable","0");
        List<Link> links = linkDao.selectList(wrapper);
        return new CommonResult(2000,"查询成功",links);
    }
}
